from django.db import models


# Create your models here.
class User(models.Model):
    user = models.CharField(max_length=30)
    name = models.CharField(max_length=30)
    sex = models.CharField(max_length=10)
    age = models.IntegerField()
    label = models.CharField(max_length=100)

    def __str__(self):
        return self.name  # 返回字段值


class IdCard(models.Model):
    number = models.CharField(max_length=20, verbose_name="卡号")
    address = models.CharField(max_length=50, default="北京")
    user = models.OneToOneField(User, on_delete=models.CASCADE)  # 定义一对一的模型关系


class Project(models.Model):
    name = models.CharField(max_length=30)
    describe = models.CharField(max_length=100, null=True)
    datetime = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'project'
        verbose_name_plural = '项目'


class App(models.Model):
    name = models.CharField(max_length=30)
    describe = models.CharField(max_length=100, null=True)
    datetime = models.DateTimeField(auto_now_add=True)
    project = models.ForeignKey(Project, on_delete=models.CASCADE)  # 定义一对多的模型关系

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'app'
        verbose_name_plural = '应用'


class Server(models.Model):
    hostname = models.CharField(max_length=30)
    ip = models.GenericIPAddressField()
    describe = models.CharField(max_length=100, null=True)
    app = models.ManyToManyField(App)

    def __str__(self):
        return self.hostname

    class Meta:
        db_table = 'server'
        verbose_name_plural = '服务器'
